home *** CD-ROM | disk | FTP | other *** search
- Path: news.bridge.net!news
- From: David Byrden <100101.2547@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Volatile-Correct; a useful concept?
- Date: 7 Jan 1996 22:19:27 GMT
- Organization: self-employed
- Message-ID: <4cpgtf$hlc@news.bridge.net>
- NNTP-Posting-Host: ppp-mia1-39.bridge.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
-
-
- A thought for multithreaded & system programmers;
-
-
- With the mainstream of programmers moving to multithreaded
- code on Win32, the 'volatile' qualifier is no longer something
- that only the writers of device drivers need use.
-
- (For those who don't recall, 'volatile' on a variable means
- that it may change due to outside forces, i.e. it is not a
- passive piece of RAM. The compiler will therefore read its
- value each time you use it in your code, not making
- optimisations.)
-
- A plain variable shared between two threads may need the
- volatile qualifier, depending on how it is used.
-
- However, I have seen no consensus of opinion on what 'volatile'
- means with respect to class objects. Only 'volatile' member
- functions may be called on an object declared 'volatile'.
- But what are we to DO with this fact? Class objects having more
- than one data member of a built-in type, are not safe to share
- between threads just because you declare them 'volatile'.
-
- I propose the concept of a class being "volatile-correct" in a
- mannar analogous to the concept of "const-correctness". A
- class object declared 'volatile' could be SAFELY USED BY
- MULTIPLE THREADS. Its volatile member functions would
- internally protect its data by mutexes.
-
- The class could have non-volatile member functions which could
- be used on object NOT declared volatile; thus, a volatile reference
- to an ordinary object oculd be passed to two threads so they
- could share it safely, but its creator would have more efficient
- access to it, without mutexes being involved.
-
- The drawback of this scheme is that a 'volatile' member function
- will treat all data in its object as volatile. This does NOT
- help "volatile-correctness"; just like with "const-correctness",
- you have to program carefully to attain it. Even worse, the volatile
- member functions will be less efficient because of this
- characteristic, a drawback which does not arise in const-correct
- class design.
-
- So; what do you think? Would it appeal to you that you could make
- an object safe for thread sharing by declaring it volatile? Or
- is this a useless concept?
-
- David Byrden
-
-
- || My opinions ARE those of my employer. ||
-
-
-
-